package cn.wolfcode.crm.web.Controller;

import cn.wolfcode.crm.Service.ISystemDictionaryItemService;
import cn.wolfcode.crm.Service.ISystemDictionaryService;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.query.SystemDictionaryItemQueryObject;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//shiro需要在用户访问响应资源的时候对其权限做检查
//因为需要在访问每个资源的时候都做检查,所以需要使用通用的代码
//shiro使用动态代理的方式来实现
//动态代理的使用场景:对目标方法做功能增强的时候(原本没有的功能.增强的时候赋予)
@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    //shiro中的value只是权限表达式,但我们将用作其他用途
    //RequiresPermissions:当前方法是否用户value中指定的的所有权限
    //如果有,运行访问,否则不能访问
    //logical:检查权限的时候,是都需要(AND),还是有一个就行(OR)
    @RequiresPermissions(value = {"明细列表","systemDictionaryItem:list"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryItemQueryObject qo) {
        if (qo.getParentId() != null){
            PageInfo<SystemDictionaryItem> pageResult = systemDictionaryItemService.queryForList(qo);
            model.addAttribute("pageResult", pageResult);
            model.addAttribute("parent",systemDictionaryService.get(qo.getParentId()));
        }
        //查询所有的字典目录
        model.addAttribute("dics",systemDictionaryService.selectAll());
        return "systemDictionaryItem/list";//跳转到list.jsp页面/WEB-INF/view/systemDictionaryItem/list.flt
    }

    @RequiresPermissions(value = {"明细保存或修改","systemDictionaryItem:saveOrUpdate"},logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionaryItem systemDictionaryItem) {
        JsonResult result = new JsonResult();
        try {
            System.out.println(1);
            if (systemDictionaryItem.getId() == null) {
                systemDictionaryItemService.insert(systemDictionaryItem);
            } else {
                systemDictionaryItemService.updateByPrimaryKey(systemDictionaryItem);
            }
        } catch (Exception e) {
                e.printStackTrace();
                result.mark("保存失败");
            }

        return result;
    }


    @RequiresPermissions(value = {"明细删除","systemDictionaryItem:delete"},logical = Logical.OR)
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult result = new JsonResult();
        try {
            if(id != null){
                systemDictionaryItemService.deleteByPrimaryKey(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("删除失败");
        }
        return result;
    }

    @RequiresPermissions(value = {"明细编辑","systemDictionaryItem:edit"},logical = Logical.OR)
    @RequestMapping("/edit")
    public String edit(Model model, Long id) {
        if (id != null) {
            SystemDictionaryItem systemDictionaryItem = systemDictionaryItemService.selectByPrimaryKey(id);
            model.addAttribute("dept", systemDictionaryItem);
        }
        return "systemDictionaryItem/input";//跳转到list.jsp页面/WEB-INF/view/systemDictionaryItem/list.flt
    }
}
